package com.sskj.functions.activity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.sskj.common.base.App;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.NumberUtil;
import com.sskj.common.util.SPUtil;
import com.sskj.functions.R;
import com.sskj.functions.R2;
import com.sskj.functions.presenter.BorrowMoneyPresenter;
import com.sskj.lib.Constans;
import com.sskj.lib.RConfig;
import com.sskj.lib.SPConfig;
import com.sskj.lib.adapter.BaseAdapter;
import com.sskj.lib.adapter.ViewHolder;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.bean.StrategyBean;
import com.sskj.lib.util.ArithUtil;
import com.sskj.lib.util.CommonUtil;
import com.zhouyou.view.seekbar.SignSeekBar;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author Hey
 * Create at  2019/06/26
 */
@Route(path = RConfig.FUNCTIONS_JBSX)
public class BorrowMoneyActivity extends BaseActivity<BorrowMoneyPresenter> {
    @BindView(R2.id.roll_out_jilu_iv)
    LinearLayout rollOutJiluIv;
    @BindView(R2.id.tvTitle)
    TextView tvTitle;
    @BindView(R2.id.roll_out_zuori_tv)
    TextView rollOutZuoriTv;
    @BindView(R2.id.roll_out_zrhl_tv)
    TextView rollOutZrhlTv;
    @BindView(R2.id.functions_shuju_ll)
    LinearLayout functionsShujuLl;
    @BindView(R2.id.roll_out_leiji_tv)
    TextView rollOutLeijiTv;
    @BindView(R2.id.roll_out_ljsy_tv)
    TextView rollOutLjsyTv;
    @BindView(R2.id.roll_out_all_tv)
    TextView rollOutAllTv;
    @BindView(R2.id.roll_out_all_iv)
    ImageView rollOutAllIv;
    @BindView(R2.id.roll_out_ll)
    LinearLayout rollOutLl;
    @BindView(R2.id.borrow_rv)
    RecyclerView borrowRv;
    @BindView(R2.id.borrow_rv_two)
    RecyclerView borrowRvTwo;
    @BindView(R2.id.smartRefreshLayout)
    SmartRefreshLayout smartRefreshLayout;
    private String pay_type = "usdt";
    private int page = 1;
    private StrategyBean data;
    private BaseAdapter<StrategyBean.List1Bean> myAdapter;
    private BaseAdapter<StrategyBean.List2Bean> adapter;
    private double progress;
    private String state = "1";
    private boolean status = true;
    private String languages = "1";

    @Override
    public int getLayoutId() {
        return R.layout.functions_activity_roll_out;
    }

    @Override
    public BorrowMoneyPresenter getPresenter() {
        return new BorrowMoneyPresenter();
    }

    @Override
    public void initView() {
        if (Constans.LANGUAGE_EN.equals(SPUtil.get(SPConfig.LANGUAGE, Constans.LANGUAGE_CNA))) {
            languages = "2";
        } else if (Constans.LANGUAGE_CNA.equals(SPUtil.get(SPConfig.LANGUAGE, Constans.LANGUAGE_CNA))) {
            languages = "1";
        } else if (Constans.LANGUAGE_TRA.equals(SPUtil.get(SPConfig.LANGUAGE, Constans.LANGUAGE_CNA))) {
            languages = "3";
        }
        tvTitle.setText(App.INSTANCE.getString(R.string.functions_borrowMoneyActivity1));
        ClickUtil.click(300, rollOutJiluIv, () -> {
            ARouter.getInstance().build(RConfig.FUNCTIONS_ROLL_RECORD).withString(Constans.TYPE, Constans.JBSX).navigation();
        });
        ClickUtil.click(300, rollOutLl, () -> {
            if (status) {
                status = false;
                borrowRvTwo.setVisibility(View.VISIBLE);
                rollOutAllIv.setVisibility(View.GONE);
            } else {
                status = true;
                rollOutAllIv.setVisibility(View.VISIBLE);
                borrowRvTwo.setVisibility(View.GONE);
            }
        });
        mPresenter.getBorrowData(pay_type, page + "", "2", languages, true);
        smartRefreshLayout.setOnRefreshListener(refreshLayout -> {
            page = 1;
            mPresenter.getBorrowData(pay_type, page + "", "2", languages, false);
        });
        smartRefreshLayout.setOnLoadMoreListener(refreshLayout -> {
            page++;
            mPresenter.getBorrowData(pay_type, page + "", "2", languages, false);
        });
        setSize();
        setData();
    }

    @Override
    public void initData() {

    }

    public static void start(Context context) {
        Intent intent = new Intent(context, BorrowMoneyActivity.class);
        context.startActivity(intent);
    }

    @SuppressLint("CheckResult")
    private void setSize() {
        borrowRv.setLayoutManager(new LinearLayoutManager(this));
        myAdapter = new BaseAdapter<StrategyBean.List1Bean>(R.layout.strategy_item_layout, null, borrowRv, true) {
            @Override
            public void bind(ViewHolder holder, StrategyBean.List1Bean item) {
//                holder.setText(R.id.tv_name, item.getDescribe());
                state = item.getStatus();
                SignSeekBar seekBar = holder.getView(R.id.seekbar);
                seekBar.setEnabled(false);
                ImageView iv = holder.getView(R.id.yigaoqing);
                LinearLayout llItem = holder.getView(R.id.strategy_item_ll);
                try {
                    double div = ArithUtil.div(Double.parseDouble(item.getSurplus()), Double.parseDouble(item.getTotal_price()), 2);
                    progress = ArithUtil.sub(100, ArithUtil.mul(div, 100));

                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
                //进度条
                seekBar.getConfigBuilder().floatType().min(0).max(100).build();
                //进图条进度
                seekBar.setProgress((int) progress);
                if (state.equals("1")) {
                    iv.setVisibility(View.GONE);
                    holder.setText(R.id.title, item.getName())
                            .setText(R.id.state, App.INSTANCE.getString(R.string.functions_strategyActivity1))
                            .setText(R.id.nianhualv, NumberUtil.keep2(item.getInterest_rate()) + "%")
                            .setText(R.id.shengyuketou, App.INSTANCE.getString(R.string.functions_strategyActivity2) + CommonUtil.dealCoinNum(item.getSurplus(), item.getPay_type().toUpperCase()) + item.getPay_type().toUpperCase())
                            .setText(R.id.time, item.getCycle() + App.INSTANCE.getString(R.string.functions_strategyActivity3))
                            .setText(R.id.strategy_progress_tv, NumberUtil.keep2(progress) + "%")
                            .setText(R.id.zonge, App.INSTANCE.getString(R.string.functions_strategyActivity4) + CommonUtil.dealCoinNum(item.getTotal_price(), item.getPay_type().toUpperCase()) + item.getPay_type().toUpperCase());
                } else {
                    return;
                }
                ClickUtil.click(300, llItem, () -> {
                    ARouter.getInstance().build(RConfig.FUNCTIONS_DETAILS).withString(Constans.TYPE, Constans.FUNCTIONS_JBSX)
                            .withSerializable("List1Bean", item)
                            .withInt("itemType", 0).navigation();
                });
            }
        };
    }

    @SuppressLint("CheckResult")
    private void setData() {
        borrowRvTwo.setLayoutManager(new LinearLayoutManager(this));
        adapter = new BaseAdapter<StrategyBean.List2Bean>(R.layout.strategy_item_layout, null, borrowRvTwo, true) {
            @Override
            public void bind(ViewHolder holder, StrategyBean.List2Bean item) {
//                holder.setText(R.id.tv_name, item.getDescribe());
                state = item.getStatus();
                if (!state.equals("1")) {
                    holder.setText(R.id.title, item.getName())
                            .setText(R.id.state, "")
                            .setText(R.id.nianhualv, NumberUtil.keep2(item.getInterest_rate()) + "%")
                            .setText(R.id.shengyuketou, App.INSTANCE.getString(R.string.functions_strategyActivity2) + CommonUtil.dealCoinNum(item.getSurplus(), item.getPay_type().toUpperCase()) + item.getPay_type().toUpperCase())
                            .setText(R.id.time, item.getCycle() + App.INSTANCE.getString(R.string.functions_strategyActivity3))
                            .setText(R.id.strategy_progress_tv, "100%")
                            .setText(R.id.zonge, App.INSTANCE.getString(R.string.functions_strategyActivity4) + CommonUtil.dealCoinNum(item.getTotal_price(), item.getPay_type().toUpperCase()) + item.getPay_type().toUpperCase());
                    SignSeekBar seekBar = holder.getView(R.id.seekbar);
                    seekBar.setEnabled(false);
                    //进度条
                    seekBar.getConfigBuilder().floatType().min(0).max(100).build();
                    //进图条进度
                    seekBar.setProgress(100);
                } else {
                    return;
                }
                LinearLayout llItem = holder.getView(R.id.strategy_item_ll);
                ClickUtil.click(300, llItem, () -> {
                    ARouter.getInstance().build(RConfig.FUNCTIONS_DETAILS).withString(Constans.TYPE, Constans.FUNCTIONS_JBSX)
                            .withSerializable("List2Bean", item)
                            .withInt("itemType", 1).navigation();
                });

            }
        };
    }

    @SuppressLint("SetTextI18n")
    public void getStrategyDataSuccess(StrategyBean data) {
        this.data = data;
        if (data != null) {
            rollOutLjsyTv.setText("≈" + NumberUtil.keep2(ArithUtil.mul(data.getExchange_rate(), Double.parseDouble(rollOutLeijiTv.getText().toString()))) + "CNY");
            rollOutZrhlTv.setText("≈" + NumberUtil.keep2(ArithUtil.mul(data.getExchange_rate(), Double.parseDouble(rollOutZuoriTv.getText().toString()))) + "CNY");
            rollOutZuoriTv.setText(CommonUtil.dealCoinNum(data.getProfit(), Constans.COIN_USDT));
            rollOutLeijiTv.setText(CommonUtil.dealCoinNum(data.getCumulative(), Constans.COIN_USDT));
            if (page == 1) {
                myAdapter.setNewData(data.getList1());
                adapter.setNewData(data.getList2());
            } else {
                myAdapter.addData(data.getList1());
                adapter.addData(data.getList2());
            }
        }
    }

    public void onStopRefresh() {
        if (smartRefreshLayout != null) {
            smartRefreshLayout.finishRefresh();
            smartRefreshLayout.finishLoadMore();
        }
    }
}
